
 <!DOCTYPE HTML>
<html lang="chinese">
<head>
  <meta charset="UTF-8">

  <title>简单的python连接mysql | liyang&#39;s blog</title>

    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=3, minimum-scale=1">
    
    <meta name="author" content="Screenshot 2015">
    
    <meta name="description" itemprop="description" content="用于个人笔记">
    
    
    
    <meta name="twitter:card" content="summary" />
    <meta name="twitter:site" content="564845354" />
    <meta name="twitter:title" content="简单的python连接mysql | liyang&#39;s blog" />
      
        <meta name="twitter:description" content="用于个人笔记" />
      
    
    
    <link rel="alternate" href="/atom.xml" title="liyang&#39;s blog" type="application/atom+xml">
    
    
    <link rel="icon" href="/img/favicon.ico">
    
    
    <link rel="apple-touch-icon" href="/http://7viklp.com1.z0.glb.clouddn.com/yilan/img/blog/pacman.jpg">
    <link rel="apple-touch-icon-precomposed" href="/http://7viklp.com1.z0.glb.clouddn.com/yilan/img/blog/pacman.jpg">
    
    <link rel="stylesheet" href="/css/style.css">
</head>

  <body>
    <header>
      <div>
		
			<div id="imglogo">
				<a href="/"><img src="/img/logo.svg" alt="liyang&#39;s blog" title="liyang&#39;s blog"/></a>
			</div>
			
			<div id="textlogo">
				<h1 class="site-name"><a href="/" title="liyang&#39;s blog">liyang&#39;s blog</a></h1>
				<h2 class="blog-motto">Stay hungry,stay foolish.</h2>
			</div>
			<div class="navbar"><a class="navbutton navmobile" href="#" title="Menu">
			</a></div>
			<nav class="animated">
				<ul>
					
						<li><a href="/">Home</a></li>
					
						<li><a href="/archives">Archives</a></li>
					
						<li><a href="/about">About</a></li>
					
					<li>
					
											<form class="search" action="/search/index.html" method="get" accept-charset="utf-8">
											<label>Search</label>
											<input type="text" class="st-default-search-input" maxlength="20" placeholder="Search" />
											</form>
					
					</li>
				</ul>
			</nav>			
			<script type="text/javascript">
			  (function(w,d,t,u,n,s,e){w['SwiftypeObject']=n;w[n]=w[n]||function(){
			  (w[n].q=w[n].q||[]).push(arguments);};s=d.createElement(t);
			  e=d.getElementsByTagName(t)[0];s.async=1;s.src=u;e.parentNode.insertBefore(s,e);
			  })(window,document,'script','//s.swiftypecdn.com/install/v2/st.js','_st');

			  _st('install','ThbyuUMiwzLDsmVUEvye','2.0.0');
			</script>
</div>

    </header>
    <div id="container">
      <div id="main" class="post" itemscope itemprop="blogPost">
	<article itemprop="articleBody"> 
		<header class="article-info clearfix">
  <h1 itemprop="name">
    
      <a href="/2016/03/08/python/" title="简单的python连接mysql" itemprop="url">简单的python连接mysql</a>
  </h1>
  <p class="article-author">By
    
      <a href="http://buildlove.github.io" title="Screenshot 2015">Screenshot 2015</a>
    </p>
  <p class="article-time">
    <time datetime="2016-03-08T06:27:06.000Z" itemprop="datePublished">2016-03-08</time>
    Updated:<time datetime="2016-10-13T09:50:08.000Z" itemprop="dateModified">2016-10-13</time>
    
  </p>
</header>
	<div class="article-content">
		
		
		<div id="toc" class="toc-article">
			<strong class="toc-title">Contents</strong>
		<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#安装virtualenv"><span class="toc-number">1.</span> <span class="toc-text">安装virtualenv</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装mysql"><span class="toc-number">2.</span> <span class="toc-text">安装mysql</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#普通连接数据库的方法"><span class="toc-number">3.</span> <span class="toc-text">普通连接数据库的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#启动数据库mysqld-exe-–console"><span class="toc-number">3.1.</span> <span class="toc-text">启动数据库mysqld.exe –console</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建数据库test"><span class="toc-number">3.2.</span> <span class="toc-text">创建数据库test</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#普通连接数据库实现增删改查"><span class="toc-number">4.</span> <span class="toc-text">普通连接数据库实现增删改查</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#引用数据库模块"><span class="toc-number">4.1.</span> <span class="toc-text">引用数据库模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#打开数据库连接-账号-密码-数据库名称"><span class="toc-number">4.2.</span> <span class="toc-text">打开数据库连接                  账号   密码    数据库名称</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用cursor-方法获取操作游标"><span class="toc-number">4.3.</span> <span class="toc-text">使用cursor()方法获取操作游标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#删（删除已经创建好的表tbTest）"><span class="toc-number">4.4.</span> <span class="toc-text">删（删除已经创建好的表tbTest）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#增-创建数据表SQL语句"><span class="toc-number">4.5.</span> <span class="toc-text">增(创建数据表SQL语句)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改（在表中插入数据）"><span class="toc-number">4.6.</span> <span class="toc-text">改（在表中插入数据）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查-获取一条数据库打印。"><span class="toc-number">4.7.</span> <span class="toc-text">查 获取一条数据库打印。</span></a></li></ol></li></ol>
		</div>
		
		<p>环境 win7 python3.4</p>
<h2 id="安装virtualenv"><a href="#安装virtualenv" class="headerlink" title="安装virtualenv"></a>安装virtualenv</h2><p>pip install virtualenv<br>创建一个目录，命令行进入目录<br>virtualenv –no-site-packages venv<br>进入venv虚拟环境<br>找到../venv/Scripts/activate这个文件<br>直接拖放到命令行小黑框内回车或者绝对路径复制粘贴到命令行回车<br>(venv) C:\Users\liyang\Desktop\github1\python\myproject&gt;<br>命令行前出现venv的就说明进入另一个独立的python环境</p>
<h2 id="安装mysql"><a href="#安装mysql" class="headerlink" title="安装mysql"></a>安装mysql</h2><p>windows系统下需要安装PyMySQL连接数据库<br>pip install PyMySQL<br>安装之后在”\myproject\venv\Lib\site-packages\pymysql__init__.py”文件夹写入<br>import pymysql<br>在myproject文件夹下创建一个item.py文件，写入：</p>
<p>import pymysql<br>conn = pymysql.connect(host=’127.0.0.1’, port=3306, user=’root’, passwd=’123456’, db=’mysql’)<br>cursor = conn.cursor()</p>
<p>#创建user表<br>cursor.execute(‘create table aaa (id varchar(20) primary key, name varchar(20))’)</p>
<p>#插入一行记录， 注意MYSQL的占位符是%s<br>cursor.execute(‘insert into aaa (id, name) values (%s, %s)’, [‘3’, ‘uncle’])</p>
<p>#提交<br>conn.commit()<br>cursor.execute(‘select * from aaa where id = %s’, [‘3’])<br>values = cursor.fetchall()<br>print(values)</p>
<p>#删除表<br>cursor.execute(“DROP TABLE IF EXISTS aaa”)</p>
<p>#关闭Cursor和connection<br>cursor.close()<br>conn.close()</p>
<p>python item.py<br>//((‘3’, ‘uncle’),)<br>不报错说明已经成功了</p>
<h2 id="普通连接数据库的方法"><a href="#普通连接数据库的方法" class="headerlink" title="普通连接数据库的方法"></a>普通连接数据库的方法</h2><h3 id="启动数据库mysqld-exe-–console"><a href="#启动数据库mysqld-exe-–console" class="headerlink" title="启动数据库mysqld.exe –console"></a>启动数据库mysqld.exe –console</h3><p>打开另一个cmd窗口mysql -u root -p输入账号密码</p>
<h3 id="创建数据库test"><a href="#创建数据库test" class="headerlink" title="创建数据库test"></a>创建数据库test</h3><p>创建表tbTest<br>create database test;<br>use test;<br>create table tbTest<br>(<br>  first_name varchar(255),<br>  last_name varchar(255),<br>  age int,<br>  sex varchar(255),<br>  income varchar(255)<br>);<br>查看内容<br>show databases;<br>show columns from tbTest;</p>
<h2 id="普通连接数据库实现增删改查"><a href="#普通连接数据库实现增删改查" class="headerlink" title="普通连接数据库实现增删改查"></a>普通连接数据库实现增删改查</h2><p>!/usr/bin/python<br> -<em>- coding: UTF-8 -</em>-</p>
<h3 id="引用数据库模块"><a href="#引用数据库模块" class="headerlink" title="引用数据库模块"></a>引用数据库模块</h3><p>import MySQLdb</p>
<h3 id="打开数据库连接-账号-密码-数据库名称"><a href="#打开数据库连接-账号-密码-数据库名称" class="headerlink" title="打开数据库连接                  账号   密码    数据库名称"></a>打开数据库连接                  账号   密码    数据库名称</h3><p>db = MySQLdb.connect(“localhost”,”root”,”123456”,”test” )</p>
<h3 id="使用cursor-方法获取操作游标"><a href="#使用cursor-方法获取操作游标" class="headerlink" title="使用cursor()方法获取操作游标"></a>使用cursor()方法获取操作游标</h3><p>cursor = db.cursor()</p>
<h3 id="删（删除已经创建好的表tbTest）"><a href="#删（删除已经创建好的表tbTest）" class="headerlink" title="删（删除已经创建好的表tbTest）"></a>删（删除已经创建好的表tbTest）</h3><p>cursor.execute(“DROP TABLE IF EXISTS tbTest”)</p>
<h3 id="增-创建数据表SQL语句"><a href="#增-创建数据表SQL语句" class="headerlink" title="增(创建数据表SQL语句)"></a>增(创建数据表SQL语句)</h3><p>sql = “””CREATE TABLE tbTest (<br>         FIRST_NAME  CHAR(20) NOT NULL,<br>         LAST_NAME  CHAR(20),<br>         AGE INT,<br>         SEX CHAR(1),<br>         INCOME FLOAT )”””<br>cursor.execute(sql)</p>
<h3 id="改（在表中插入数据）"><a href="#改（在表中插入数据）" class="headerlink" title="改（在表中插入数据）"></a>改（在表中插入数据）</h3><p>sql = “””INSERT INTO tbTest(FIRST_NAME,<br>         LAST_NAME, AGE, SEX, INCOME)<br>         VALUES (‘Mac’, ‘Mohan’, 20, ‘M’, 2000)”””<br>try:<br>  cursor.execute(sql)<br>  db.commit()<br>except:<br>  db.rollback()</p>
<h3 id="查-获取一条数据库打印。"><a href="#查-获取一条数据库打印。" class="headerlink" title="查 获取一条数据库打印。"></a>查 获取一条数据库打印。</h3><p>data = cursor.fetchone()<br>print “Database version : %s “ % data</p>
  
	</div>
		<footer class="article-footer clearfix">

  <div class="article-tags">
  
  <span></span> <a href="/tags/python/">python</a><a href="/tags/mysql/">mysql</a>
  </div>




<div class="article-share" id="share">

  <div data-url="http://buildlove.github.io/2016/03/08/python/" data-title="简单的python连接mysql | liyang&#39;s blog" data-tsina="null" class="share clearfix">
  </div>

</div>
</footer>   	       
	</article>
	
<nav class="article-nav clearfix">
 
 <div class="prev" >
 <a href="/2016/03/24/mac-python/" title="python2.7更新到3.4下载pip报错">
  <strong>上一篇:</strong><br/>
  <span>
  python2.7更新到3.4下载pip报错</span>
</a>
</div>


<div class="next">
<a href="/2016/01/07/javascript/"  title="javascript">
 <strong>下一篇:</strong><br/> 
 <span>javascript
</span>
</a>
</div>

</nav>

	
<section class="comment">
	<div class="ds-thread"></div>
</section>

</div>  
      <div class="openaside"><a class="navbutton" href="#" title="Show Sidebar"></a></div>

  <div id="toc" class="toc-aside">
  <strong class="toc-title">Contents</strong>
  <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#安装virtualenv"><span class="toc-number">1.</span> <span class="toc-text">安装virtualenv</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装mysql"><span class="toc-number">2.</span> <span class="toc-text">安装mysql</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#普通连接数据库的方法"><span class="toc-number">3.</span> <span class="toc-text">普通连接数据库的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#启动数据库mysqld-exe-–console"><span class="toc-number">3.1.</span> <span class="toc-text">启动数据库mysqld.exe –console</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建数据库test"><span class="toc-number">3.2.</span> <span class="toc-text">创建数据库test</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#普通连接数据库实现增删改查"><span class="toc-number">4.</span> <span class="toc-text">普通连接数据库实现增删改查</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#引用数据库模块"><span class="toc-number">4.1.</span> <span class="toc-text">引用数据库模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#打开数据库连接-账号-密码-数据库名称"><span class="toc-number">4.2.</span> <span class="toc-text">打开数据库连接                  账号   密码    数据库名称</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#使用cursor-方法获取操作游标"><span class="toc-number">4.3.</span> <span class="toc-text">使用cursor()方法获取操作游标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#删（删除已经创建好的表tbTest）"><span class="toc-number">4.4.</span> <span class="toc-text">删（删除已经创建好的表tbTest）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#增-创建数据表SQL语句"><span class="toc-number">4.5.</span> <span class="toc-text">增(创建数据表SQL语句)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#改（在表中插入数据）"><span class="toc-number">4.6.</span> <span class="toc-text">改（在表中插入数据）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#查-获取一条数据库打印。"><span class="toc-number">4.7.</span> <span class="toc-text">查 获取一条数据库打印。</span></a></li></ol></li></ol>
  </div>

<div id="asidepart">
<div class="closeaside"><a class="closebutton" href="#" title="Hide Sidebar"></a></div>
<aside class="clearfix">

  
<div class="categorieslist">
	<p class="asidetitle">Categories</p>
		<ul>
		
			<li><a href="/categories/database/" title="database">database<sup>1</sup></a></li>
		
			<li><a href="/categories/git-difftool-Beyond-Compare/" title="git difftool(Beyond Compare)">git difftool(Beyond Compare)<sup>1</sup></a></li>
		
			<li><a href="/categories/hexo学习配置/" title="hexo学习配置">hexo学习配置<sup>1</sup></a></li>
		
			<li><a href="/categories/javascript/" title="javascript">javascript<sup>1</sup></a></li>
		
			<li><a href="/categories/linux/" title="linux">linux<sup>1</sup></a></li>
		
			<li><a href="/categories/node-npm命令行参数和插件/" title="node&amp;npm命令行参数和插件">node&amp;npm命令行参数和插件<sup>1</sup></a></li>
		
			<li><a href="/categories/python/" title="python">python<sup>1</sup></a></li>
		
			<li><a href="/categories/七牛/" title="七牛">七牛<sup>1</sup></a></li>
		
			<li><a href="/categories/分布式版本管理工具/" title="分布式版本管理工具">分布式版本管理工具<sup>1</sup></a></li>
		
			<li><a href="/categories/数据库/" title="数据库">数据库<sup>1</sup></a></li>
		
			<li><a href="/categories/绘图/" title="绘图">绘图<sup>1</sup></a></li>
		
		</ul>
</div>


  
<div class="tagslist">
	<p class="asidetitle">Tags</p>
		<ul class="clearfix">
		
			<li><a href="/tags/database/" title="database">database<sup>2</sup></a></li>
		
			<li><a href="/tags/git/" title="git">git<sup>1</sup></a></li>
		
			<li><a href="/tags/hexo/" title="hexo">hexo<sup>1</sup></a></li>
		
			<li><a href="/tags/js/" title="js">js<sup>1</sup></a></li>
		
			<li><a href="/tags/linux/" title="linux">linux<sup>1</sup></a></li>
		
			<li><a href="/tags/mac/" title="mac">mac<sup>1</sup></a></li>
		
			<li><a href="/tags/mysql/" title="mysql">mysql<sup>1</sup></a></li>
		
			<li><a href="/tags/node/" title="node">node<sup>1</sup></a></li>
		
			<li><a href="/tags/npm/" title="npm">npm<sup>1</sup></a></li>
		
			<li><a href="/tags/photo/" title="photo">photo<sup>1</sup></a></li>
		
			<li><a href="/tags/ps/" title="ps">ps<sup>1</sup></a></li>
		
			<li><a href="/tags/python/" title="python">python<sup>2</sup></a></li>
		
			<li><a href="/tags/ubuntu/" title="ubuntu">ubuntu<sup>1</sup></a></li>
		
			<li><a href="/tags/分布式版本管理工具/" title="分布式版本管理工具">分布式版本管理工具<sup>1</sup></a></li>
		
			<li><a href="/tags/图床/" title="图床">图床<sup>1</sup></a></li>
		
		</ul>
</div>


  <div class="rsspart">
	<a href="/atom.xml" target="_blank" title="rss">RSS</a>
</div>

</aside>
</div>
    </div>
    <footer><div id="footer" >
	
	<div class="line">
		<span></span>
		<div class="author"></div>
	</div>
	
	
	<section class="info">
		<p> Hello ,I&#39;m Liyang Page in Honglian. <br/>
			This is my blog,believe it or not.</p>
	</section>
	 
	<div class="social-font clearfix">
		
		<a href="http://weibo.com/1954002307" target="_blank" title="weibo"></a>
		
		
		<a href="https://twitter.com/564845354" target="_blank" title="twitter"></a>
		
		
		<a href="https://github.com/buildlove" target="_blank" title="github"></a>
		
		
		<a href="https://www.facebook.com/profile.php?id=100008166199067" target="_blank" title="facebook"></a>
		
		
	</div>
		<p class="copyright">Powered by <a href="http://hexo.io" target="_blank" title="hexo">hexo</a> and Theme by <a href="https://github.com/A-limon/pacman" target="_blank" title="Pacman">Pacman</a> © 2017 
		
		<a href="http://buildlove.github.io" target="_blank" title="Screenshot 2015">Screenshot 2015</a>
		
		</p>
</div>
</footer>
    <script src="/js/jquery-2.1.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.navbar').click(function(){
    $('header nav').toggleClass('shownav');
  });
  var myWidth = 0;
  function getSize(){
    if( typeof( window.innerWidth ) == 'number' ) {
      myWidth = window.innerWidth;
    } else if( document.documentElement && document.documentElement.clientWidth) {
      myWidth = document.documentElement.clientWidth;
    };
  };
  var m = $('#main'),
      a = $('#asidepart'),
      c = $('.closeaside'),
      o = $('.openaside');
  $(window).resize(function(){
    getSize(); 
    if (myWidth >= 1024) {
      $('header nav').removeClass('shownav');
    }else
    {
      m.removeClass('moveMain');
      a.css('display', 'block').removeClass('fadeOut');
      o.css('display', 'none');
      
      $('#toc.toc-aside').css('display', 'none');
        
    }
  });
  c.click(function(){
    a.addClass('fadeOut').css('display', 'none');
    o.css('display', 'block').addClass('fadeIn');
    m.addClass('moveMain');
  });
  o.click(function(){
    o.css('display', 'none').removeClass('beforeFadeIn');
    a.css('display', 'block').removeClass('fadeOut').addClass('fadeIn');      
    m.removeClass('moveMain');
  });
  $(window).scroll(function(){
    o.css("top",Math.max(80,260-$(this).scrollTop()));
  });
});
</script>

<script type="text/javascript">
$(document).ready(function(){ 
  var ai = $('.article-content>iframe'),
      ae = $('.article-content>embed'),
      t  = $('#toc'),
      h  = $('article h2')
      ah = $('article h2'),
      ta = $('#toc.toc-aside'),
      o  = $('.openaside'),
      c  = $('.closeaside');
  if(ai.length>0){
    ai.wrap('<div class="video-container" />');
  };
  if(ae.length>0){
   ae.wrap('<div class="video-container" />');
  };
  if(ah.length==0){
    t.css('display','none');
  }else{
    c.click(function(){
      ta.css('display', 'block').addClass('fadeIn');
    });
    o.click(function(){
      ta.css('display', 'none');
    });
    $(window).scroll(function(){
      ta.css("top",Math.max(140,320-$(this).scrollTop()));
    });
  };
});
</script>


<script type="text/javascript">
$(document).ready(function(){ 
  var $this = $('.share'),
      url = $this.attr('data-url'),
      encodedUrl = encodeURIComponent(url),
      title = $this.attr('data-title'),
      tsina = $this.attr('data-tsina');
  var html = [
  '<a href="#" class="overlay" id="qrcode"></a>',
  '<div class="qrcode clearfix"><span>扫描二维码分享到微信朋友圈</span><a class="qrclose" href="#share"></a><strong>Loading...Please wait</strong><img id="qrcode-pic" data-src="http://s.jiathis.com/qrcode.php?url=' + encodedUrl + '"/></div>',
  '<a href="#textlogo" class="article-back-to-top" title="Top"></a>',
  '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  '<a href="#qrcode" class="article-share-qrcode" title="QRcode"></a>',
  '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  '<a href="http://service.weibo.com/share/share.php?title='+title+'&url='+encodedUrl +'&ralateUid='+ tsina +'&searchPic=true&style=number' +'" class="article-share-weibo" target="_blank" title="Weibo"></a>',
  '<span title="Share to"></span>'
  ].join('');
  $this.append(html);
  $('.article-share-qrcode').click(function(){
    var imgSrc = $('#qrcode-pic').attr('data-src');
    $('#qrcode-pic').attr('src', imgSrc);
    $('#qrcode-pic').load(function(){
        $('.qrcode strong').text(' ');
    });
  });
});     
</script>


<script type="text/javascript">
  var duoshuoQuery = {short_name:"buildlove"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
    || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
</script> 



<link rel="stylesheet" href="/fancybox/jquery.fancybox.css" media="screen" type="text/css">
<script src="/fancybox/jquery.fancybox.pack.js"></script>
<script type="text/javascript">
$(document).ready(function(){ 
  $('.article-content').each(function(i){
    $(this).find('img').each(function(){
      if ($(this).parent().hasClass('fancybox')) return;
      var alt = this.alt;
      if (alt) $(this).after('<span class="caption">' + alt + '</span>');
      $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
    });
    $(this).find('.fancybox').each(function(){
      $(this).attr('rel', 'article' + i);
    });
  });
  if($.fancybox){
    $('.fancybox').fancybox();
  }
}); 
</script>



  </body>
</html>
